//## 자재  정보 저장 ####################
function fnSave()
{
	if (!$("#srcNm").val())
	{
		alert("자재 이름을 바르게 입력해 주세요!");
		$("#srcNm").focus();
		return;
	}
	var valUrl  = '/action/src/save.asp';
		var param = "srcCd="+$("#srcCd").val();
		param += "&srcNm="+encodeURIComponent($("#srcNm").val());
		param += "&srcSeq="+$("#srcSeq").val();
		param += "&prdInfo="+encodeURIComponent($("#prdInfo").val());
		param += "&remark="+encodeURIComponent($("#remark").val());
		param += "&flag="+$("#flag").val();
		param += "&type="+$(':radio[name="type"]:checked').val();
	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				fnReset();
				grid01List();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 자재  정보 그리드 표시 ####################

var G_grid01Rslt = "" //XML Data를 받을 전역변수 선언

function grid01List()
{
	clstTable("Grid01");
	$("#tr01Result").css("display","");
	setNullRow("loadSpan","1");
	var valUrl = "/action/src/list.asp";
	var param = "findItem="+encodeURIComponent($("#findItem").val());
		  param += "&findType="+$("#findType").val();

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());

			G_grid01Rslt = xml;
			setGrid01(1);
			fnReset();
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

function setGrid01(curPage)
{
	clstTable("Grid01");

	$("#tr01Result").css("display","none");
	$("#loadSpan1").html("");
	var nodeCnt = $(G_grid01Rslt).find("Source").find("Src").length;

	if(nodeCnt == 0)
	{
		$("#tr01Result").css("display","");
		$("#loadSpan1").html("검색된 자료가 없습니다.");
		$("#grid01Cnt").html("0");
		return;
	}
	var listNum = 50;
	var totalCount = nodeCnt;
	$("#grid01Cnt").html(totalCount);
	if (curPage == "") curPage = 1;
	var totalPage = Math.ceil(nodeCnt/listNum);
	var restArticle = (nodeCnt%listNum);
	startNo = (parseInt(curPage)-1) * listNum;	
	var endNo = startNo + listNum;
	
	nodeCnt = nodeCnt - ((curPage-1)*listNum);
	if (totalPage == curPage)
	{
		if (restArticle == 0) var endNo = startNo + listNum;
		else var endNo = startNo + restArticle;
	}

	var pages = fnPaging("Grid01", curPage, totalPage);

	$("#pageDiv01").html(pages);
	
	var grid01Array = new Array();
	var chkCnt = 0;
	for(var j=startNo; j<endNo; j++)
	{
		var srcCd		= $(G_grid01Rslt).find("Source").find("Src").eq(j).find("srcCd").text();
		var srcNm		= $(G_grid01Rslt).find("Source").find("Src").eq(j).find("srcNm").text();
		var restQty		= $(G_grid01Rslt).find("Source").find("Src").eq(j).find("restQty").text();
		var srcSeq		= $(G_grid01Rslt).find("Source").find("Src").eq(j).find("srcSeq").text();
		var prdInfo			= $(G_grid01Rslt).find("Source").find("Src").eq(j).find("prdInfo").text();
		var type			= $(G_grid01Rslt).find("Source").find("Src").eq(j).find("type").text();
		var flag				= $(G_grid01Rslt).find("Source").find("Src").eq(j).find("flag").text();

		ji = j+1;
		
		var showFlag = "";
		var tagFirst = "";
		var tagLast = "";
		if(flag == "Y") showFlag = "유효";
		else showFlag = "삭제";
		
		if (flag == "N") { var bgColor = "#F2F2F2"; tagFirst = "<font color=\"#CCCCCC\">"; tagLast = "</font>"; }
		else var bgColor = "#FFFFFF";
		
		var vType = "자재";
		if (type == "T") vType = "툴";
		if (prdInfo == "") prdInfo = "&nbsp;";

		grid01Array[0] = ji;
		grid01Array[1] = tagFirst + srcCd + tagLast;
		grid01Array[2] = tagFirst + srcNm + tagLast;
		grid01Array[3] = tagFirst + restQty + tagLast;
		grid01Array[4] = tagFirst + srcSeq + tagLast;
		grid01Array[5] = tagFirst + vType + tagLast;
		grid01Array[6] = tagFirst + prdInfo + tagLast;
		grid01Array[7] = showFlag;

		var boldLine = ""
		if (ji%10 == 0 && ji != 1) boldLine = "Y"
		
		mkTable2("Grid01", grid01Array, bgColor, ",,,,,,left,", "fnSetSrc('"+srcCd+"')", boldLine);
	}
}


//## 자재코드 상세정보 표시 ####################
function fnSetSrc(srcCd)
{
	var valUrl = "/action/src/view.asp";
	var param = "srcCd="+srcCd;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());
			fnReset();			
			$("#srcCd").val($(xml).find("srcCd").text());
			$("#srcNm").val($(xml).find("srcNm").text());
			$("#srcSeq").val($(xml).find("srcSeq").text());
			$("#remark").val($(xml).find("remark").text());
			$("#prdInfo").val($(xml).find("prdInfo").text());
			$("input:radio[name='type']:radio[value='"+$(xml).find("type").text()+"']").prop("checked",true);
			$("#flag").val($(xml).find("flag").text());

		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

//## 자재  정보 Reset 기능
function fnReset()
{
	$("#srcCd").val("");
	$("#srcNm").val("");
	$("#srcSeq").val("");
	$("#prdInfo").val("");
	$("#remark").val("");
	$("#flag").val("Y");
}


//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findItem").val("");
}